package meijuli.com.join51.business.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息中心
 *
 * @author jack 2017-05-12 17:54
 */
public class MessageActivity extends BaseActivity implements View.OnClickListener{
    Button mAnnoButton;
    Button mMsgButton;
    ViewPager mViewPager;
    Toolbar mToolbar;
    TextView mTextView;
    private List<Fragment> mFragmentList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        mAnnoButton = findViewById(R.id.btn_announcement);
        mMsgButton = findViewById(R.id.btn_Notice);
        mViewPager = findViewById(R.id.view_pager);
        mToolbar = findViewById(R.id.toolbar);
        mTextView = findViewById(R.id.toolbar_title);
        mAnnoButton.setOnClickListener(this);
        mMsgButton.setOnClickListener(this);
        mTextView.setText("消息中心");
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mFragmentList = new ArrayList<>();
        mFragmentList.add(MessageFragment.newInstance(MessageFragment.MESSAGE));
        mFragmentList.add(MessageFragment.newInstance(MessageFragment.ANNOUNCEMENT));
        mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
        mMsgButton.setSelected(true);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0) {
                    mMsgButton.setSelected(true);
                    mAnnoButton.setSelected(false);
                } else {
                    mAnnoButton.setSelected(true);
                    mMsgButton.setSelected(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_announcement:
                mViewPager.setCurrentItem(1);
                mAnnoButton.setSelected(true);
                mMsgButton.setSelected(false);
                break;
            case R.id.btn_Notice:
                mViewPager.setCurrentItem(0);
                mMsgButton.setSelected(true);
                mAnnoButton.setSelected(false);
                break;
        }
    }

    private class FragmentAdapter extends FragmentPagerAdapter {

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }
}
